package com.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.common.entity.A_adminer;

public class AuthorityFilter implements Filter {
	private String[] filters={
			"login.jsp",
			"login1.jsp",
			"loginServlet",
			"loginimg",
			"logincss",
			"loginjsp",
			"images"
	};
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	};

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest req=(HttpServletRequest) request;
		String uri=req.getRequestURI();
		String contextPath=req.getContextPath();
		String url=uri.substring(contextPath.length()+1);
		boolean isFlag=false;
		for(String str:filters){
			if(url.startsWith(str)){
				isFlag=true;
				break;
			}
		}
		if(isFlag){
			chain.doFilter(request, response);
		}else{
			HttpSession session=req.getSession();
			A_adminer adminer = (A_adminer)session.getAttribute("userInfo");
			if(adminer == null){
				request.setAttribute("msg", "�����¼���ܷ���");
				request.getRequestDispatcher("login.jsp").forward(request, response);;
			}else{
				chain.doFilter(request, response);
			}
		}
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

}
